package org.projectwithoutname.ui.view;

import java.util.ArrayList;

import model.Episode;
import model.EpisodesPackEpisode;
import model.Season;
import model.SeasonsPackSeason;
import model.Serie;
import model.SeriesPackSerie;

import org.eclipse.emf.common.util.EList;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.IHandlerService;
import org.projectwithoutname.commons.ICommandIds;
import org.projectwithoutname.ui.util.Hide;

public class EpisodesView extends SuperView {
	public static final String ID = "org.ProjectWithoutName.application.episodesView";
	private final int WIDTH = 112, HEIGHT = 63;
	private Composite composite;
	private ArrayList<Button> episodes = new ArrayList<Button>();
	private String filePath;
	 
	/**
     * We will set up all the serie's episodes here and link with a real path  
     * name on disk to disponibilizar play they inside the view of visualization. 
     */
    public void createPartControl(Composite parent) {
    	composite = parent;
    	Device device = composite.getDisplay();
		Color color = new Color(device, 122, 153, 194);
		composite.setBackground(color);
		composite.setLayout(null);
		
		composite.addControlListener(new ControlListener() {
			
			@Override
			public void controlResized(ControlEvent e) {
				organizedButtons();
			}
			
			@Override
			public void controlMoved(ControlEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
	}
    /**
     * FIX to find episodes throw  episodes package
     */
    public void initView(){
    	for(Episode episode : root.getEpisodes()){
        	CreateButton(episode);
    	}
    }
    
    public void CreateButton(final Episode episode){
		final Button button = new Button(composite, SWT.NONE);
		button.setText(episode.getName() != null? episode.getName() :"");
		hookDoubleClickCommand(button, episode.getName());
		episodes.add(button);
		organizedButtons();
	}
    
    private void hookDoubleClickCommand(final Button button, final String name) {
		button.setFocus();
		button.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseUp(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseDown(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				for (Episode episode : root.getEpisodes()){
					if (episode.getName().equals(name))
						setFilePath(episode.getPath());
				}
				IHandlerService handlerService = (IHandlerService) getSite().getService(IHandlerService.class);
				try {
					handlerService.executeCommand(ICommandIds.PLAY, null);
				} catch (Exception ex) {
					ex.printStackTrace();
					throw new RuntimeException(ICommandIds.PLAY + " not found");
				}
				
			}
		});
	}
    
    protected void setFilePath(String path) {
		filePath = path;
		
	}
    
    public String getFilePath(){
    	return filePath;
    }
	/**
     * Simulate a grid layout using a setBounds's technique. It used to organize 
     * the episodes inside the view when the control is create or resized.   
     */
    private void organizedButtons(){
		int i = 0, x = 10, y = 10;
		int lineSize =(composite.getSize().x / (WIDTH + 20)) < 1? 1:composite.getSize().x /(WIDTH + 20);
		
		for (Button button : episodes){
			if (i<lineSize){
				button.setBounds(x, y, WIDTH, HEIGHT);
				x += WIDTH + 20;
				i++;
			}
			else{
				i=1;
				x = 10;
				y += HEIGHT + 20;
				button.setBounds(x, y, WIDTH, HEIGHT);
				x += WIDTH + 20;
			}
		}
	}

	
    public void setFocus() {
//		viewer.getControl().setFocus();
	}

	public Hide getHideManager() {
		return super.getHide();
	}
}